<?php
/**
 * created by zzy
 * date: 2017/11/17 10:39
 */


namespace App\Http\Admin\Process\Controllers;

use App\Eloquent\Ygt\ProcessFieldCategory;
use App\Eloquent\Ygt\ProcessFieldCompany;
use App\Engine\Func;
use Framework\BaseClass\Http\Admin\Controller;
use App\Eloquent\Ygt\ProcessField;
use App\Eloquent\Ygt\Category;
use DB;

class DictController extends Controller
{
    public function lists()
    {
        $adminUser         = request()->user('admin')->toArray();
        $companyId         = $adminUser['company_id'];

        if (request()->isMethod('post')) {

            $type           = request( 'type', 0 );
            $title          = request( 'title', '' );
            $where          = [];
            if( $type )
            {
                $where['type']      = $type;
            }
            if( $title )
            {
                $where['title']      = ['like','%'.$title.'%'];
            }
            $lists          = ProcessField::getCompanyFieldList( $companyId, $where );
            $result         = collect($lists)->toJson();
            return $result;
        } else {

            $toolbarItem       = 'export';
            if( $companyId == 1 )
            {
                $toolbarItem   .= ',add';
            }
            $fieldTypeList   = ProcessField::getFieldTypeList( 4 );
//            $typeListJson    = ProcessField::getTypeList( 4 );
//            $typeList         = ProcessField::getTypeList(5);
            $where              = ['company_id'=>$companyId];
            $fieldCategoryList	= ProcessFieldCategory::getRows($where)->toArray();
            $typeListJson       = Func::getIdTitleByType( $fieldCategoryList, 4 );
            $typeList           = Func::getIdTitleByType( $fieldCategoryList, 5 );
            return $this->view('lists', compact('fieldTypeList','typeList','typeListJson','toolbarItem') );
        }
    }
    public function edit()
    {
        $adminUser         = request()->user('admin')->toArray();
        $companyId         = $adminUser['company_id'];

        $params             = $this->requestJson();
        $resultArr          = ProcessField::editCompanyField($companyId, $params);
        //$resultArr          = ['status'=>true,'id'=>75];
        $result             = $resultArr['status'];
        if( $result === 'exist' ){
            return $this->ajaxFail('名字已存在');
        } else if( $result === true ){
            //$result             = $resultArr['status'];
            //return $this->ajaxSuccess('成功');
            $data                 = $resultArr['data'];
            $data['statusCode'] = 200;
            $data['message']    = '成功';
            $json                = json_encode($data);
            return $json;
        } else {
            return $this->ajaxFail('失败');
        }
    }
    public function category()
    {
        $adminUser         = request()->user('admin')->toArray();
        $companyId         = $adminUser['company_id'];
        $fieldId           = request('field_id',0);
        if (request()->isMethod('post')) {
            $categoryId        = request('category_id',0);
            if( !$fieldId || !$categoryId ){
                return $this->ajaxFail('参数不全');
            }
            $where              = ['company_id'=>$companyId, 'field_id'=>$fieldId];
            $data               = ['category_id'=>$categoryId];
            $result             = ProcessFieldCompany::updateOneData($where,$data);
            if( $result ){
                return $this->ajaxSuccess('成功');
            } else {
                return $this->ajaxFail('失败');
            }
        } else {
            $where              = ['company_id'=>$companyId,'field_id'=>$fieldId];
            $fieldInfo          = ProcessFieldCompany::getInfo($where)->toArray();
            $categoryModel      = new Category();
            $categoryList       = $categoryModel->getChildrenList(2,false,$companyId);
            $data               = [
                'field_info'=>$fieldInfo, 'category_list'=>$categoryList
            ];
            return $this->view('category', compact('data') );
        }
    }

    public function categoryListAjax(){
        $adminUser         = request()->user('admin')->toArray();
        $companyId         = $adminUser['company_id'];
        $fieldId           = request('field_id',0);
        $pid        = request('pid',0);
        $cids        = request('cids',0);

        $where              = ['company_id'=>$companyId, 'field_id'=>$fieldId];
        $data               = ['category_id'=>$pid,'children_category_id'=>$cids];
        $result             = ProcessFieldCompany::updateOneData($where,$data);
        if( $result ){
            return $this->ajaxSuccess('成功');
        } else {
            return $this->ajaxFail('失败');
        }
    }

    public function categoryList(){
        $adminUser         = request()->user('admin')->toArray();
        $companyId         = $adminUser['company_id'];

        if (request()->isMethod('post')) {
            $pid          = request('pid',0);

            $permission = new Category();
            $roleList = $permission->getCategoryOption($pid,$companyId,[],1);
            foreach($roleList as $key=>$val){
                if($val['pid'] == $pid){
                    $roleList[$key]['pid'] = 0;
                }
            }
            return ['list'=>$roleList,'pageSize'=>10000];
        }
        else{
            $fieldId           = request('field_id',0);
            if($fieldId){
                $where              = ['company_id'=>$companyId, 'field_id'=>$fieldId];
                $result             = ProcessFieldCompany::getOneData($where);
                $old_category_id = $result['category_id'];
                $children_category_id = $result['children_category_id'];
            }

            //获得材料分类下的一级分类
            $permission = new Category();
            $roleList = $permission->whereIn('company_id',[0,$companyId])->where('pid','=',2)->where('display','=',1)->get()->toArray();
            if(!isset($old_category_id)){
                $old_category_id = $roleList[0]['id'];
            }

            foreach($roleList as $key=>$val){
                if($val['id'] == $old_category_id){
                    $roleList[$key]['selected'] = 'selected="selected"';
                }
                else{
                    $roleList[$key]['selected'] = '';
                }
            }
            $data = ['children_category_id'=>$children_category_id,'old_category_id'=>$old_category_id,'field_id'=>$fieldId,'role_list'=>$roleList];
        }

        return $this->view('category-list',$data);
    }


}